import numpy
import pylab
from genutils.multi_objective_ga import MogaPopulation
from genutils.io import from_config
import time
import os
from run_adex_sim import score_model

class ThisPop(MogaPopulation):
    def score_model(self,gene, plot_results=False):
        t1 = time.time()
        if plot_results:
            run_line = ("python run_adex_sim.py %f %f %f %f %f %f %f plot_results 2>/dev/null" % 
                    (gene[0], gene[1], gene[2], gene[3], gene[4], gene[5], gene[6]))
        else:
            run_line = ("python run_adex_sim.py %f %f %f %f %f %f %f 2>/dev/null" % 
                    (gene[0], gene[1], gene[2], gene[3], gene[4], gene[5], gene[6]))
        
        sim_results = os.popen(run_line).read().split('\n')[1]
        t2 = time.time()
        print "found result %s in time (%2.3f)" % (sim_results, t2-t1)
        sim_results = eval(sim_results)
        return sim_results

# read in parameters
cfile = "moga_adex_fit.parameters"
num_generations         = int(from_config('num_generations',cfile))
population_size         = int(from_config('population_size',cfile))
initial_population_size = int(from_config('initial_population_size',cfile))

# initalize the genetic algorithm population
thispop = ThisPop(population_size,initial_population_size,cfile)

thispop.evolve(10, 20)
score_model(thispop.generation[-1][0], plot_results=True)









